<template>
  <div class="login">
    <van-nav-bar title="登录" left-arrow @click-left="goBack" />
    
    <div class="login-form">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="username"
            name="username"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
        </van-cell-group>
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit">
            登录
          </van-button>
        </div>
      </van-form>
      
      <div class="register-link">
        <van-button plain hairline type="primary" @click="goToRegister">
          没有账号？立即注册
        </van-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Toast } from 'vant'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const username = ref('')
    const password = ref('')
    
    const goBack = () => {
      router.go(-1)
    }
    
    const goToRegister = () => {
      router.push('/register')
    }
    
    const onSubmit = (values) => {
      // 这里应该调用登录API
      Toast.loading('登录中...')
      
      // 模拟登录成功
      setTimeout(() => {
        Toast.success('登录成功')
        router.push('/')
      }, 1000)
    }
    
    return {
      username,
      password,
      goBack,
      goToRegister,
      onSubmit
    }
  }
}
</script>

<style scoped>
.login-form {
  margin-top: 50px;
}

.register-link {
  text-align: center;
  margin-top: 20px;
}
</style>